/* eslint-disable */
/**
 * Created by tangqiaojie <2482366539@qq.com> on 2020/4/7 0007
 */
import {openFakeWin,sendSbList,logout} from './tool.js';
import { insertMoment,nextTick } from '../../lib/utils.js';
import { saveTableDetail } from './asyncTable.js';
import { getQysdsLi } from './qysds.js';
import { fixZeroInfo } from '../../lib/zeroReportUtils.js'
let currentZeroInfo;
const pages={
  "index":"etax.chongqing.chinatax.gov.cn/sword",
  "step1":"etax.chongqing.chinatax.gov.cn:8001/WLSB/WLSBBS/TTAX_index.html?MODULE=TTAX_WBZR.html",
  "step2":"etax.chongqing.chinatax.gov.cn:6011/sword",
  "step3":"etax.chongqing.chinatax.gov.cn:8001/WLSB/WLSBBS/TTAX_index.html?MODULE=TTAX_SB_JCSB.html"
}

const isPage=(name)=>{
  return window.location.href.includes(pages[name])
}

const handleLogin=(info)=>{
  nextTick(()=>{
    let username=$("#username"),password=$("#password");
    if(username.length && password.length){
      username.val(info.nationalTaxUser);
      password.val(info.nationalTaxPass);
      return true;
    }
  })
};

const handleSb=(currentZeroInfo)=>{
  insertMoment();
  nextTick(()=> {
    if (window.moment) {
      sendSbList(currentZeroInfo);
      setTimeout(() => {
        logout()
      }, 2000);
      return true;
    }
  });
}

const getTables=()=>{

};

const handleSmallIndex=(currentZeroInfo)=>{
  nextTick(()=>{
    let tds = $("#wddb tr");
    let qysds = getQysdsLi();
    if(tds.length){
      let td=$("#wddb tr:eq(1) td:eq(0)");
      if(td.html()==="增值税申报" && window.sessionStorage.getItem('zeroStep')==1){
        window.sessionStorage.setItem('zeroStep',2);
        openFakeWin('null', 'F251000200000033','增值税申报', '102231');
      }else if(td.html()==="附加税（费）申报" && window.sessionStorage.getItem('zeroStep')==2){
        window.sessionStorage.setItem('zeroStep',3);
        openFakeWin('sword?ctrl=SB246FjsfnssbCtrl_initView', 'F251000100000089','附加税（费）申报', '100171')
      }else if(qysds && $(qysds).find('td:eq(2)').text() === '未申报' && $(qysds).find('td:eq(3)').text() === '办理') {
        $(qysds).find('td:eq(3) button').click();
      } else {
        handleSb(currentZeroInfo);
      }
      return true;
    }
  }, 500);
}

const getZzsTable=()=>{
  nextTick(()=>{
    let iWin=$("#indexContent")[0].contentWindow;
    if(iWin && iWin.$){
      let bWin=iWin.$("#bizContent")[0].contentWindow;
      if(bWin && bWin.$ && bWin.$("#datagrid-row-r1-2-0 .grid_button1").length){
        bWin.editPZZL(1,2,0,1);
        getZzsTable1(iWin,bWin);
        return true;
      }
    }
  })
}

const getZzsTable1=(iWin)=>{
  nextTick(()=>{
    let tableBtn=iWin.$("#datagrid-row-r2-2-1 td[field='BB_MC'] .datagrid-cell");
    if(tableBtn){
      tableBtn.click();
      setTimeout(()=>{
        tableBtn.dblclick();
        nextTick(()=>{
          let win1=iWin.$('#BB_0102000000')[0].contentWindow;
          console.log(win1.$("h3").html());
          /*saveTableDetail({
            declareType: '增值税一般纳税人申报',
            title: win.$('h3').html().trim(),
            html: frameWin.document.querySelector('#Table_all').innerHTML.replace(/\s+/g, ''),
            css: ''
          });*/
        },500)
      },500)
      return true;
    }
  },1000)
}

const handleSmallStep3=()=>{
  console.log(333);
  //getZzsTable();

  /*let win=$("#indexContent")[0].contentWindow;
  nextTick(()=>{
    console.log($("table.ui_state_focus .ui_state_highlight"));
    if($("table.ui_state_focus .ui_state_highlight").length){
      if($("table.ui_state_focus .ui_state_highlight").val() === "填表申报"){
        $("table.ui_state_focus .ui_state_highlight").click();
        tableMainSave1(win);
        return true
      }else{
        setTimeout(()=>{
          window.location.reload();
        },3000)
      }
    }
  })*/
  /*insertMoment();
  nextTick(()=>{
    if(window.moment){
      sendSbList(currentZeroInfo);
      return true;
    }
  })*/
}

const handleSmallStep2=()=>{
  nextTick(()=>{
    let btns=document.getElementsByClassName("submit_ie67_btn");
    if(btns && btns.length && btns[1]){
      btns[1].click();
      nextTick(()=>{
        let yesBtn=document.getElementById("ymPrompt_btn_0");
        if(yesBtn){
          yesBtn.click();
          window.setTimeout(()=>{
            let el=document.getElementsByClassName("ym-content");
            if(el.length && el[0].innerText.includes("保存成功")){
              window.close();
            }
          },2000)
          return true;
        }
      },1000)
      return true;
    }
  },8000)
}

const handleSmallStep1=()=>{
  let opents=$("[name='OpenTS']")[0];
  if(opents && opents.contentWindow && opents.contentWindow.uf_setNoTips){
    opents.contentWindow.uf_setNoTips();
  }
  let win=$("#indexContent")[0].contentWindow;
  nextTick(()=>{
    if($("table.ui_state_focus .ui_state_highlight").length){
      if($("table.ui_state_focus .ui_state_highlight").val() === "填表申报"){
        $("table.ui_state_focus .ui_state_highlight").click();
        tableMainSave1(win);
        return true
      }else{
        setTimeout(()=>{
          window.location.reload();
        },3000)
      }
    }
  })
}

const startShenbao=(currentZeroInfo)=>{
  if($('.ui_state_lock .ui_state_highlight').length && $('.ui_state_lock .ui_state_highlight').val()==="确定"){
    $('.ui_state_lock .ui_state_highlight').click();
    nextTick(()=>{
      if($(".ui_state_full iframe").length && $(".ui_state_full iframe").contents().find("#fullResult").length){
        if($(".ui_state_full iframe").contents().find("#fullResult").html().includes('申报已经成功')){
          // fixZeroInfo
          let recordList = currentZeroInfo.recordList;
          let zzsItme = recordList.find(item => item.declareName.includes('增值税'));
          zzsItme.isSuccess = true;
          fixZeroInfo({recordList: [zzsItme]});
          setTimeout(() => {
            window.location.href = 'https://etax.chongqing.chinatax.gov.cn/sword?ctrl=WBLoginGdgjCtrl_openLayout3Win';
          }, 1000);
        }
        return;
      }
    },15000)
  }
}

/*const getTables=(win)=>{
  nextTick(()=>{
    let tableBtn=win.$("#datagrid-row-r2-2-2 td[field='BB_MC'] .datagrid-cell");
    if(tableBtn){
      tableBtn.click();
      setTimeout(()=>{
        tableBtn.dblclick();
        nextTick(()=>{
          let frameWin=win.$("#BB_0102000001")[0].contentWindow;
          saveTableDetail({
            declareType: '增值税一般纳税人申报',
            title: win.$('h3').html().trim(),
            html: frameWin.document.querySelector('#Table_all').innerHTML.replace(/\s+/g, ''),
            css: ''
          });
        },500)
      },500)
      return true;
    }
  },1000)
}*/

const tableMainSave1=(win, currentZeroInfo)=>{
    nextTick(()=>{
      if(!$(".blockOverlay").length){
        win.$("#return_save").click();
        nextTick(()=>{
          if($('.ui_state_highlight').length && $('.ui_state_highlight').val()==="确定"){
            $('.ui_state_highlight').click();
            setTimeout(()=>{
              win.$("#BB_0102000000")[0].contentWindow.clos_XW_5();
              nextTick(()=>{
                if($('.ui_state_lock .ui_state_highlight').length && $('.ui_state_lock .ui_state_highlight').val()==="确定"){
                  $('.ui_state_highlight').click();
                  setTimeout(()=> {
                    win.$("#return_sb").click();
                    nextTick(() => {
                      if ($('.ui_state_lock .ui_state_highlight').length && $('.ui_state_lock .ui_state_highlight').val() === "确定") {
                        if ($('.ui_state_lock .ui_content span:eq(0)').length && $('.ui_state_lock .ui_content span:eq(0)').html() === "抄报税") {
                          $('.ui_state_lock .ui_state_highlight').click();
                          setTimeout(() => {
                            startShenbao(currentZeroInfo);
                          }, 1000);
                        } else {
                          startShenbao(currentZeroInfo);
                        }
                        return true;
                      }
                    }, 2000)
                  })
                  return true;
                }
                  /*if($('.ui_state_lock .ui_content span:eq(0)').length && $('.ui_state_lock .ui_content span:eq(0)').html()==="抄报税"){
                    $('.ui_state_lock .ui_state_highlight').click();
                    setTimeout(()=>{
                      startShenbao();
                    },1000);
                  }else{
                    startShenbao();
                  }
                  return true;
                }*/
              },2000)
            },1000)
            return true;
          }
        })
        return true;
      }
    },3000)
}



export {
  handleSmallIndex,
  tableMainSave1,
  handleSmallStep1,
  handleSmallStep2,
  handleSmallStep3,
};
